

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/fluid.png">
  <link rel="icon" href="/img/fluid.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="hz">
  <meta name="keywords" content="">
  
    <meta name="description" content="背景从腾讯电子签在现网问题反馈中，我们发现中发现有部分用户吐槽一些按钮不小心快速多次点击出现出现多个类似的页面，处理完业务之后回退需要退两次。虽说对于业务流程来说没有影响，但是确实影响到用户体验，所以如何优雅且强制性规范的在小程序上做防重复点击是本文研究的重点。 分析腾讯电子签小程序基于 uni-app 研发，举个栗子，通常一处存在点击的处理事件长这个样子，见下面的@click。 12345678">
<meta property="og:type" content="article">
<meta property="og:title" content="小程序点击防抖哪家强">
<meta property="og:url" content="https://blog.brzhang.club/2022/03/16/%E5%B0%8F%E7%A8%8B%E5%BA%8F%E7%82%B9%E5%87%BB%E9%98%B2%E6%8A%96%E5%93%AA%E5%AE%B6%E5%BC%BA/index.html">
<meta property="og:site_name" content="心随我动">
<meta property="og:description" content="背景从腾讯电子签在现网问题反馈中，我们发现中发现有部分用户吐槽一些按钮不小心快速多次点击出现出现多个类似的页面，处理完业务之后回退需要退两次。虽说对于业务流程来说没有影响，但是确实影响到用户体验，所以如何优雅且强制性规范的在小程序上做防重复点击是本文研究的重点。 分析腾讯电子签小程序基于 uni-app 研发，举个栗子，通常一处存在点击的处理事件长这个样子，见下面的@click。 12345678">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://blog.brzhang.club/2022/03/16/%E5%B0%8F%E7%A8%8B%E5%BA%8F%E7%82%B9%E5%87%BB%E9%98%B2%E6%8A%96%E5%93%AA%E5%AE%B6%E5%BC%BA/image-20220316185014558.png">
<meta property="og:image" content="https://blog.brzhang.club/2022/03/16/%E5%B0%8F%E7%A8%8B%E5%BA%8F%E7%82%B9%E5%87%BB%E9%98%B2%E6%8A%96%E5%93%AA%E5%AE%B6%E5%BC%BA/image-20220316190601141.png">
<meta property="og:image" content="https://blog.brzhang.club/2022/03/16/%E5%B0%8F%E7%A8%8B%E5%BA%8F%E7%82%B9%E5%87%BB%E9%98%B2%E6%8A%96%E5%93%AA%E5%AE%B6%E5%BC%BA/image-20220316191301667.png">
<meta property="og:image" content="https://blog.brzhang.club/2022/03/16/%E5%B0%8F%E7%A8%8B%E5%BA%8F%E7%82%B9%E5%87%BB%E9%98%B2%E6%8A%96%E5%93%AA%E5%AE%B6%E5%BC%BA/image-20220316191949544.png">
<meta property="article:published_time" content="2022-03-16T09:28:20.000Z">
<meta property="article:modified_time" content="2022-11-28T01:25:08.267Z">
<meta property="article:author" content="hz">
<meta property="article:tag" content="小程序">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="https://blog.brzhang.club/2022/03/16/%E5%B0%8F%E7%A8%8B%E5%BA%8F%E7%82%B9%E5%87%BB%E9%98%B2%E6%8A%96%E5%93%AA%E5%AE%B6%E5%BC%BA/image-20220316185014558.png">
  
  
    <meta name="referrer" content="no-referrer-when-downgrade">
  
  
  <title>小程序点击防抖哪家强 - 心随我动</title><meta name="robots" content="noindex">

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"blog.brzhang.club","root":"/","version":"1.9.5","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"left","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"follow_dnt":true,"baidu":null,"google":{"measurement_id":null},"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml","include_content_in_search":true};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
  


  
<meta name="generator" content="Hexo 6.3.0"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 30vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>Brzhang</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                <span>首页</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                <span>归档</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                <span>分类</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                <span>标签</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                <span>关于</span>
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              <i class="iconfont icon-search"></i>
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">
              <i class="iconfont icon-dark" id="color-toggle-icon"></i>
            </a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/img/default.png') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="小程序点击防抖哪家强"></span>
          
        </div>

        
          
  <div class="mt-3">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-author" aria-hidden="true"></i>
        hz
      </span>
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2022-03-16 17:28" pubdate>
          2022年3月16日 下午
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          5.3k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          45 分钟
        
      </span>
    

    
    
      
        <span id="busuanzi_container_page_pv" style="display: none">
          <i class="iconfont icon-eye" aria-hidden="true"></i>
          <span id="busuanzi_value_page_pv"></span> 次
        </span>
        
      
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="padding-left: 2rem; margin-right: -1rem">
    <div id="toc">
  <p class="toc-header">
    <i class="iconfont icon-list"></i>
    <span>目录</span>
  </p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <h1 id="seo-header">小程序点击防抖哪家强</h1>
            
            
              <div class="markdown-body">
                
                <h4 id="背景"><a href="#背景" class="headerlink" title="背景"></a>背景</h4><p>从<strong>腾讯电子签</strong>在现网问题反馈中，我们发现中发现有部分用户吐槽一些按钮不小心快速多次点击出现出现多个类似的页面，处理完业务之后回退需要退两次。虽说对于业务流程来说没有影响，但是确实影响到用户体验，所以如何优雅且<strong>强制性</strong>规范的在小程序上做防重复点击是本文研究的重点。</p>
<h4 id="分析"><a href="#分析" class="headerlink" title="分析"></a>分析</h4><p>腾讯电子签小程序基于 uni-app 研发，举个栗子，通常一处存在点击的处理事件长这个样子，见下面的@click。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><code class="hljs vue">&lt;template&gt;<br>  &lt;PdfItem v-for=&quot;(img, idx) in images&quot; @click=&quot;doSomeThing(idx)&quot; /&gt;<br>&lt;/template&gt;<br>&lt;script&gt;<br><br>export default &#123;<br><br>  data() &#123;<br>    return &#123;<br>      ...<br>    &#125;;<br>  &#125;,<br>  methods: &#123;<br>    doSomeThing(idx) &#123;<br>      //do some thing<br>    &#125;,<br>  &#125;,<br>&#125;;<br>&lt;/script&gt;<br></code></pre></td></tr></table></figure>

<p>那么，如何做好并消灭防重复点击就是本文要研究的点。拿上面的例子来说，大概有几个方式</p>
<ol>
<li><p>在 doSomeThing 中加一个变量，当做门卫，如果方法没处理完，再次进入直接返回，恩，每个方法都需要增加一段逻辑，去设置变量，比较繁琐。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-title function_">doSomeThing</span>(<span class="hljs-params"></span>)&#123;<br>  <span class="hljs-keyword">if</span>(isDoing)&#123;<br>    <span class="hljs-keyword">return</span><br>  &#125;<br>  isDoing = <span class="hljs-literal">true</span><br>  ....<br>  isDoing = <span class="hljs-literal">false</span><br>&#125;<br></code></pre></td></tr></table></figure>
</li>
<li><p>给 doSomeThing 方法增加一个 debounce 包裹一下，恩，比第一种略好，更加简洁点。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-attr">doSomeThing</span>:_.<span class="hljs-title function_">debounce</span>(<span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)&#123;<br>  ....<br>&#125;)<br></code></pre></td></tr></table></figure>
</li>
<li><p>hook 的方式 hook 住 click 方法，然后来做处理逻辑，无奈小程序也没有类似 web 的这种 addEventListener，遂放弃。</p>
</li>
</ol>
<p>找到一种好的解决方式不是难问题，难问题是：</p>
<ol>
<li>我们的业务代码中，已经出现了很多出没有增加防抖逻辑包裹的代码，难道线上遇到一个反馈，解决一个吗？</li>
<li>如何限制后续编码过程中，大家写 onclick 代码的时候，强制加上 onclick。</li>
</ol>
<p>很显然，对于代码规范检测，我们非常容易想到使用 eslint，但是现有的 eslint 有这么规则吗？自然是没有的，所以，我们遇到的问题在理论上转换为了：<strong>如何开发一款插件有效的检测 onClick 有没有使用防抖</strong>。</p>
<h4 id="eslint-插件开搞"><a href="#eslint-插件开搞" class="headerlink" title="eslint 插件开搞"></a>eslint 插件开搞</h4><h5 id="基本开发准备"><a href="#基本开发准备" class="headerlink" title="基本开发准备"></a>基本开发准备</h5><p>插件开发的整体流程我就不在本文的介绍当中，本文只关心如何去实现检测的逻辑及基本的程序入口和调试技巧介绍。首先，我们要检测的文件是 .vue 文件，因此，我们需要在 eslint 大环境下解析 vue 语法，那么使用什么方式解析 vue 语法，我们需要自己开发吗，显然不是，vue 肯定是有的，比如<a target="_blank" rel="noopener" href="https://github.com/vuejs/vue-eslint-parser#readme">vue-eslint-parser</a>,想要更加直观的看解析效果，可以到<a target="_blank" rel="noopener" href="https://astexplorer.net/">astexplorer</a>贴上一段 vue 代码看看。</p>
<p><img src="/2022/03/16/%E5%B0%8F%E7%A8%8B%E5%BA%8F%E7%82%B9%E5%87%BB%E9%98%B2%E6%8A%96%E5%93%AA%E5%AE%B6%E5%BC%BA/image-20220316185014558.png" srcset="/img/loading.gif" lazyload alt="image-20220316185014558" loading="lazy" data-src="/2022/03/16/%E5%B0%8F%E7%A8%8B%E5%BA%8F%E7%82%B9%E5%87%BB%E9%98%B2%E6%8A%96%E5%93%AA%E5%AE%B6%E5%BC%BA/image-20220316185014558.png"></p>
<p>那么，我们就可以插件的开发框架下，模拟 vue 文件被这个插件解析，然后生成一个语法树抛给 eslint 插件入口，参见下面的代码块，解析好的语法数会已 node 参数传入到 Program 函数。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@fileoverview</span> onClick should wrap with debounce in page</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@author</span> <span class="hljs-variable">hoollyzhang</span></span><br><span class="hljs-comment"> */</span><br><span class="hljs-meta">&#x27;use strict&#x27;</span>;<br><br><span class="hljs-comment">//------------------------------------------------------------------------------</span><br><span class="hljs-comment">// Rule Definition</span><br><span class="hljs-comment">//------------------------------------------------------------------------------</span><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&#x27;电子签防抖插件已加载&#x27;</span>)<br><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@type</span> &#123;<span class="hljs-type">import(&#x27;eslint&#x27;).Rule.RuleModule</span>&#125;</span><br><span class="hljs-comment"> */</span><br><span class="hljs-variable language_">module</span>.<span class="hljs-property">exports</span> = &#123;<br>  <span class="hljs-attr">meta</span>: &#123;<br>    <span class="hljs-attr">type</span>: <span class="hljs-string">&#x27;problem&#x27;</span>, <span class="hljs-comment">// `problem`, `suggestion`, or `layout`</span><br>    <span class="hljs-attr">docs</span>: &#123;<br>      <span class="hljs-attr">description</span>: <span class="hljs-string">&#x27;onClick should wrap with debounce in page&#x27;</span>,<br>      <span class="hljs-attr">category</span>: <span class="hljs-string">&#x27;Possible Problems&#x27;</span>,<br>      <span class="hljs-attr">recommended</span>: <span class="hljs-literal">true</span>,<br>      <span class="hljs-attr">url</span>: <span class="hljs-literal">null</span>, <span class="hljs-comment">// URL to the documentation page for this rule</span><br>    &#125;,<br>    <span class="hljs-attr">schema</span>: [], <span class="hljs-comment">// Add a schema if the rule has options</span><br>    <span class="hljs-attr">messages</span>: &#123;<br>      <span class="hljs-attr">missingAddDebounceClick</span>: <span class="hljs-string">`onClick method should wrap with debounce, use &quot;import &#123; debounce &#125; from &#x27;@/utils/custom-lodash&#x27;&quot; and wrap with it in the page.`</span>,<br>      <span class="hljs-attr">shouldNotUseEmitThisWay</span>: <span class="hljs-string">`It is suggested to use the debounce method of wrapping`</span><br>    &#125;<br>  &#125;,<br><br>  <span class="hljs-title function_">create</span>(<span class="hljs-params">context</span>) &#123;<br>    <span class="hljs-comment">// variables should be defined here</span><br><br>    <span class="hljs-comment">//----------------------------------------------------------------------</span><br>    <span class="hljs-comment">// Helpers</span><br>    <span class="hljs-comment">//----------------------------------------------------------------------</span><br><br>    <span class="hljs-comment">//----------------------------------------------------------------------</span><br>    <span class="hljs-comment">// Public</span><br>    <span class="hljs-comment">//----------------------------------------------------------------------</span><br><br>    <span class="hljs-keyword">return</span> &#123;<br>      <span class="hljs-comment">// visitor functions for different types of nodes</span><br>      <span class="hljs-title class_">Program</span>(node) &#123;<br>        <span class="hljs-keyword">const</span> fileName = context.<span class="hljs-title function_">getFilename</span>();<br>        <span class="hljs-keyword">if</span>(!fileName.<span class="hljs-title function_">endsWith</span>(<span class="hljs-string">`vue`</span>))&#123;<br>          <span class="hljs-keyword">return</span><br>        &#125;<br>        <span class="hljs-comment">//...</span><br>        <span class="hljs-keyword">const</span> &#123; templateBody,body &#125; = node<br><br>        [...methods].<span class="hljs-title function_">map</span>(<span class="hljs-function"><span class="hljs-params">item</span>=&gt;</span><span class="hljs-title function_">reportError</span>(item))<br>      &#125;,<br>    &#125;;<br>  &#125;,<br>&#125;;<br><br></code></pre></td></tr></table></figure>

<p>插件开发框架为了便于我们开发调试插件，提供了这样一套模板方法</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><code class="hljs javascript"><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@fileoverview</span> Import subPackage config in page</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@author</span> <span class="hljs-variable">hoollyzhang</span></span><br><span class="hljs-comment"> */</span><br><span class="hljs-meta">&quot;use strict&quot;</span>;<br><br><span class="hljs-comment">//------------------------------------------------------------------------------</span><br><span class="hljs-comment">// Requirements</span><br><span class="hljs-comment">//------------------------------------------------------------------------------</span><br><br><span class="hljs-keyword">const</span> rule = <span class="hljs-built_in">require</span>(<span class="hljs-string">&quot;../../../lib/rules/check-on-click-debounce&quot;</span>);<br><span class="hljs-keyword">const</span> <span class="hljs-variable constant_">VUE_ESLINT_PARSER</span> = path.<span class="hljs-title function_">join</span>(<br>  __dirname,<br>  <span class="hljs-variable constant_">NODE_MODULES</span>,<br>  <span class="hljs-string">&quot;vue-eslint-parser&quot;</span><br>);<br><br><span class="hljs-comment">//------------------------------------------------------------------------------</span><br><span class="hljs-comment">// Tests</span><br><span class="hljs-comment">//------------------------------------------------------------------------------</span><br><br><span class="hljs-keyword">const</span> ruleTester = <span class="hljs-keyword">new</span> <span class="hljs-title class_">RuleTester</span>();<br>ruleTester.<span class="hljs-title function_">run</span>(<span class="hljs-string">&quot;check-on-click-debounce&quot;</span>, rule, &#123;<br>  <span class="hljs-attr">valid</span>: [<span class="hljs-string">&quot;pages/vue-onclick-debounce/onclick-debounce-test-ok.vue&quot;</span>].<span class="hljs-title function_">map</span>(<br>    <span class="hljs-function">(<span class="hljs-params">filename</span>) =&gt;</span> (&#123;<br>      <span class="hljs-attr">code</span>: fs.<span class="hljs-title function_">readFileSync</span>(path.<span class="hljs-title function_">join</span>(<span class="hljs-variable constant_">TEST_INPUTS_DIR</span>, filename), <span class="hljs-string">&quot;utf8&quot;</span>),<br>      <span class="hljs-attr">filename</span>: path.<span class="hljs-title function_">join</span>(<span class="hljs-variable constant_">TEST_INPUTS_DIR</span>, filename),<br>      <span class="hljs-attr">parser</span>: <span class="hljs-variable constant_">VUE_ESLINT_PARSER</span>,<br>      parserOptions,<br>    &#125;)<br>  ),<br><br>  <span class="hljs-attr">invalid</span>: [<span class="hljs-string">&quot;pages/vue-onclick-debounce/onclick-debounce-test-not-ok.vue&quot;</span>].<span class="hljs-title function_">map</span>(<br>    <span class="hljs-function">(<span class="hljs-params">filename</span>) =&gt;</span> (&#123;<br>      <span class="hljs-attr">code</span>: fs.<span class="hljs-title function_">readFileSync</span>(path.<span class="hljs-title function_">join</span>(<span class="hljs-variable constant_">TEST_INPUTS_DIR</span>, filename), <span class="hljs-string">&quot;utf8&quot;</span>),<br>      <span class="hljs-attr">filename</span>: path.<span class="hljs-title function_">join</span>(<span class="hljs-variable constant_">TEST_INPUTS_DIR</span>, filename),<br>      <span class="hljs-attr">parser</span>: <span class="hljs-variable constant_">VUE_ESLINT_PARSER</span>,<br>      parserOptions,<br>      <span class="hljs-attr">errors</span>: [&#123; <span class="hljs-attr">messageId</span>: <span class="hljs-string">&quot;missingAddDebounceClick&quot;</span> &#125;],<br>    &#125;)<br>  ),<br>&#125;);<br></code></pre></td></tr></table></figure>

<p>所以，我们可以使用这个验证我们研发的 eslint 插件是否符合预期，而且调试起来也比较方便，在 vscode 上开发的话，配置一个启动项即可，比如我配置了一个 launch.json;</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><code class="hljs json"><span class="hljs-punctuation">&#123;</span><br>  <span class="hljs-comment">// 使用 IntelliSense 了解相关属性。</span><br>  <span class="hljs-comment">// 悬停以查看现有属性的描述。</span><br>  <span class="hljs-comment">// 欲了解更多信息，请访问: https://go.microsoft.com/fwlink/?linkid=830387</span><br>  <span class="hljs-attr">&quot;version&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;0.2.0&quot;</span><span class="hljs-punctuation">,</span><br>  <span class="hljs-attr">&quot;configurations&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span><br>    <span class="hljs-punctuation">&#123;</span><br>      <span class="hljs-attr">&quot;type&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;pwa-node&quot;</span><span class="hljs-punctuation">,</span><br>      <span class="hljs-attr">&quot;request&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;launch&quot;</span><span class="hljs-punctuation">,</span><br>      <span class="hljs-attr">&quot;name&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;Launch onClick test&quot;</span><span class="hljs-punctuation">,</span><br>      <span class="hljs-attr">&quot;skipFiles&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span><span class="hljs-string">&quot;&lt;node_internals&gt;/**&quot;</span><span class="hljs-punctuation">]</span><span class="hljs-punctuation">,</span><br>      <span class="hljs-attr">&quot;program&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;$&#123;workspaceFolder&#125;/packages/eslint-plugin-tsign-weapp/tests/lib/rules/check-on-click-debounce.js&quot;</span><br>    <span class="hljs-punctuation">&#125;</span><br>  <span class="hljs-punctuation">]</span><br><span class="hljs-punctuation">&#125;</span><br></code></pre></td></tr></table></figure>

<p>这样一来，我们就可以展开调试了。</p>
<p><img src="/2022/03/16/%E5%B0%8F%E7%A8%8B%E5%BA%8F%E7%82%B9%E5%87%BB%E9%98%B2%E6%8A%96%E5%93%AA%E5%AE%B6%E5%BC%BA/image-20220316190601141.png" srcset="/img/loading.gif" lazyload alt="image-20220316190601141" loading="lazy" data-src="/2022/03/16/%E5%B0%8F%E7%A8%8B%E5%BA%8F%E7%82%B9%E5%87%BB%E9%98%B2%E6%8A%96%E5%93%AA%E5%AE%B6%E5%BC%BA/image-20220316190601141.png"></p>
<h5 id="检测原理介绍"><a href="#检测原理介绍" class="headerlink" title="检测原理介绍"></a>检测原理介绍</h5><p>1、首先我们需要拿到所有&lt;template 中被 @click 引用到的方法，这个可以通过递归遍历 templateBody 这个树解决。</p>
<p><img src="/2022/03/16/%E5%B0%8F%E7%A8%8B%E5%BA%8F%E7%82%B9%E5%87%BB%E9%98%B2%E6%8A%96%E5%93%AA%E5%AE%B6%E5%BC%BA/image-20220316191301667.png" srcset="/img/loading.gif" lazyload alt="image-20220316191301667" loading="lazy" data-src="/2022/03/16/%E5%B0%8F%E7%A8%8B%E5%BA%8F%E7%82%B9%E5%87%BB%E9%98%B2%E6%8A%96%E5%93%AA%E5%AE%B6%E5%BC%BA/image-20220316191301667.png"></p>
<p>如上图，我们只需要找到这个 argument name 是 click 的这个结点中 value 里面的 expression 的 name，就是我们要检测的方法</p>
<p>2、基于上面找到的方法，我们在去 methods 的结点中去找到对应的方法，看看这个方法的 body 有没有被 debounce 包裹即可，如果没有哦就告警处理。</p>
<h5 id="实际效果"><a href="#实际效果" class="headerlink" title="实际效果"></a>实际效果</h5><p><img src="/2022/03/16/%E5%B0%8F%E7%A8%8B%E5%BA%8F%E7%82%B9%E5%87%BB%E9%98%B2%E6%8A%96%E5%93%AA%E5%AE%B6%E5%BC%BA/image-20220316191949544.png" srcset="/img/loading.gif" lazyload alt="image-20220316191949544" loading="lazy" data-src="/2022/03/16/%E5%B0%8F%E7%A8%8B%E5%BA%8F%E7%82%B9%E5%87%BB%E9%98%B2%E6%8A%96%E5%93%AA%E5%AE%B6%E5%BC%BA/image-20220316191949544.png"></p>
<!-- flag of hidden posts -->
                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/categories/%E6%8A%80%E6%9C%AF/" class="category-chain-item">技术</a>
  
  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/tags/%E5%B0%8F%E7%A8%8B%E5%BA%8F/" class="print-no-link">#小程序</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>小程序点击防抖哪家强</div>
      <div>https://blog.brzhang.club/2022/03/16/小程序点击防抖哪家强/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>hz</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2022年3月16日</div>
        </div>
      
      
      
        <div class="license-meta-item">
          <div>许可协议</div>
          <div>
            
              
              
                <a class="print-no-link" target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
                  <span class="hint--top hint--rounded" aria-label="BY - 署名">
                    <i class="iconfont icon-by"></i>
                  </span>
                </a>
              
            
          </div>
        </div>
      
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                  </article>
                </div>
              
            </div>

            
  
  
    <article id="comments" lazyload>
      
  <script type="text/javascript">
    Fluid.utils.loadComments('#comments', function() {
      var light = 'github-light';
      var dark = 'github-dark';
      var schema = document.documentElement.getAttribute('data-user-color-scheme');
      if (schema === 'dark') {
        schema = dark;
      } else {
        schema = light;
      }
      window.UtterancesThemeLight = light;
      window.UtterancesThemeDark = dark;
      var s = document.createElement('script');
      s.setAttribute('src', 'https://utteranc.es/client.js');
      s.setAttribute('repo', 'bravekingzhang/bravekingzhang.github.io');
      s.setAttribute('issue-term', 'pathname');
      
      s.setAttribute('label', 'utterances');
      
      s.setAttribute('theme', schema);
      s.setAttribute('crossorigin', 'anonymous');
      document.getElementById('comments').appendChild(s);
    })
  </script>
  <noscript>Please enable JavaScript to view the comments</noscript>


    </article>
  


          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>
  </div>
</div>





  



  



  



  



  


  
  





  <script>
  Fluid.utils.createScript('https://lib.baomitu.com/mermaid/8.14.0/mermaid.min.js', function() {
    mermaid.initialize({"theme":"default"});

    Fluid.utils.listenDOMLoaded(function() {
      Fluid.events.registerRefreshCallback(function() {
        if ('mermaid' in window) {
          mermaid.init();
        }
      });
    });
  });
</script>






    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
    </div>
  
  
    <div class="statistics">
  
  

  
    
      <span id="busuanzi_container_site_pv" style="display: none">
        总访问量 
        <span id="busuanzi_value_site_pv"></span>
         次
      </span>
    
    
      <span id="busuanzi_container_site_uv" style="display: none">
        总访客数 
        <span id="busuanzi_value_site_uv"></span>
         人
      </span>
    
    
  
</div>

  
  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.4/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    <script  src="/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.20.1/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init(Object.assign({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      scrollSmooth    : true,
      includeTitleTags: true,
      headingsOffset  : -boardTop,
    }, CONFIG.toc));
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }

    Fluid.events.registerRefreshCallback(function() {
      if ('tocbot' in window) {
        tocbot.refresh();
        var toc = jQuery('#toc');
        if (toc.length === 0 || !tocbot) {
          return;
        }
        if (toc.find('.toc-list-item').length > 0) {
          toc.css('visibility', 'visible');
        }
      }
    });
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.11/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));

    Fluid.events.registerRefreshCallback(function() {
      if ('anchors' in window) {
        anchors.removeAll();
        var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
        var res = [];
        for (var item of el) {
          res.push('.markdown-body > ' + item.trim());
        }
        if (CONFIG.anchorjs.placement === 'left') {
          anchors.options.class = 'anchorjs-link-left';
        }
        anchors.add(res.join(', '));
      }
    });
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  
      <script>
        if (!window.MathJax) {
          window.MathJax = {
            tex    : {
              inlineMath: { '[+]': [['$', '$']] }
            },
            loader : {
              load: ['ui/lazy']
            },
            options: {
              renderActions: {
                insertedScript: [200, () => {
                  document.querySelectorAll('mjx-container').forEach(node => {
                    let target = node.parentNode;
                    if (target.nodeName.toLowerCase() === 'li') {
                      target.parentNode.classList.add('has-jax');
                    }
                  });
                }, '', false]
              }
            }
          };
        } else {
          MathJax.startup.document.state(0);
          MathJax.texReset();
          MathJax.typeset();
          MathJax.typesetPromise();
        }

        Fluid.events.registerRefreshCallback(function() {
          if ('MathJax' in window && MathJax.startup.document && typeof MathJax.startup.document.state === 'function') {
            MathJax.startup.document.state(0);
            MathJax.texReset();
            MathJax.typeset();
            MathJax.typesetPromise();
          }
        });
      </script>
    

  <script  src="https://lib.baomitu.com/mathjax/3.2.2/es5/tex-mml-chtml.js" ></script>

  <script  src="/js/local-search.js" ></script>

  <script defer src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" ></script>





<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
</body>
</html>
